﻿#include "qqlaunch.h"

QQLaunch::QQLaunch(QObject *parent)
    : QObject{parent}
    , trayIcon (new QSystemTrayIcon(QIcon(":/background/imagesRes/QQ_icon.png"),this))
{
    //设置系统图标
    trayIcon->show();
    trayIconMenu = new TrayIconMenu();
    connect(trayIconMenu,&TrayIconMenu::quit,this,&QQLaunch::quit);
    connect(trayIconMenu,&TrayIconMenu::logout,this,&QQLaunch::logout);
    trayIcon->setContextMenu(trayIconMenu);

    connect(trayIcon, &QSystemTrayIcon::activated, [this](QSystemTrayIcon::ActivationReason reason){
        switch (reason) {
        case QSystemTrayIcon::Trigger:
        case QSystemTrayIcon::DoubleClick:{
            if (qq!=nullptr&&!qq->isVisible()) {// 检查窗口是否可见，如果不可见则显示它
                qq->show();
                qq->raise(); // 将窗口带到所有其他窗口的前面
                qq->activateWindow(); // 激活窗口
            }
            else if(qqmain!=nullptr&&!qqmain->isVisible()){
                qqmain->show();
                qqmain->raise();
                qqmain->activateWindow();
            }
        }break;
        case QSystemTrayIcon::Context:{
            trayIconMenu->activateWindow();
            trayIconMenu->show();
        }break;
        default:{
        }
        }
    });

    qq=new QQ();
    qq->show();
    loadQQRespond();
}

void QQLaunch::loadQQRespond(){
    connect(qq,&QQ::login,this,&QQLaunch::login);
    connect(qq,&QQ::quit,this,&QQLaunch::quit);
}
void QQLaunch::loadQQMainRespond(){
    connect(qqmain,&QQMain::quit,[this](){
        delete qqmain;
        qqmain=nullptr;
        emit quit();
    });

    connect(qqmain,&QQMain::showComm,[this](const QString &email,const QString &name,const QString &message){
        static bool action=false;
        if(!communication->isVisible()&&action){
            //屏幕居中显示
            QPoint screenCenter = QApplication::primaryScreen()->geometry().center();
            QPoint windowSize(communication->minimumWidth()/2,communication->minimumHeight()/2);
            communication->move(screenCenter-windowSize);
            //重置大小
            communication->resize(0,0);
        }
        //显示
        communication->insertFirendList(email,name,message);
        if(communication->isMinimized()){
            if(communication->isMax){
                communication->setMaxStatus();
            }
            else{
                communication->setNormalStatus();
            }
        }
        communication->show();
        action=true;
    });
}
void QQLaunch::loadCommunicationRespond(){
    connect(communication,&Communication::hideComm,[this](){
        communication->hide();
    });
}
void QQLaunch::login(const QString &wsAddr,const QString &email,const QString &username,const bool &isDownLoad,const QString &sign,const QString &token){
    qq->deleteLater();
    qq=nullptr;
    HttpClient *httpClient=new HttpClient("http://127.0.0.1:10400",token,this);
    WebSocketClient* webSocketClient=nullptr;
    FileOperator* fileOperator=new FileOperator(this);
    SQLiteUtil* sQLiteUtil=new SQLiteUtil(QString("./accountData/%1/database.db").arg(email),this);
    //初始化数据库表
    sQLiteUtil->initDataBaseDB();
    //连接聊天服务器
    {
        QVariantMap resultMap=sQLiteUtil->selectOneMessageRecentCreateTime();
        QString lastTime=QString::number(QDateTime::currentMSecsSinceEpoch());
//        if(!resultMap.isEmpty())
//            lastTime=resultMap["createTime"].toString();
//        else
//            lastTime="0";
        webSocketClient=new WebSocketClient(QString("ws://%1/ChatScope?email=%2&lastTime=%3").arg(wsAddr,email,lastTime),this);
        connect(webSocketClient,&WebSocketClient::logout,this,&QQLaunch::logout);
    }

    qqmain=new QQMain(httpClient,webSocketClient,fileOperator,sQLiteUtil,email,username,isDownLoad,sign);
    communication=new Communication(httpClient,webSocketClient,fileOperator,sQLiteUtil,email);

    qqmain->show();

    loadQQMainRespond();
    loadCommunicationRespond();

    trayIconMenu->insertLogoutButton();
}

void QQLaunch::logout(){
    trayIconMenu->removeLogoutButton();
    if(qqmain!=nullptr){
        delete qqmain;
        qqmain=nullptr;
    }
    if(qq==nullptr){
        qq=new QQ();
        qq->show();
        loadQQRespond();
    }
    if(trayIconMenu->isVisible()){
        trayIconMenu->hide();
    }
}
